//
//  Alu_LineShowView.m
//  LineShowDome
//
//  Created by lirilu on 15/6/24.
//  Copyright (c) 2015年 glirlu@gmail.com. All rights reserved.
//

#import "Alu_LineShowView.h"
#import "Alu_PointInfo.h"

@interface Alu_LineShowView()


@end

@implementation Alu_LineShowView

- (void)initData
{
    NSLog(@"init data");
    //初始化
    
    /**
     *  lineWidth
     *  lineColor
     */
    if (self.delegate != nil) {
        self.lineColor = [self.delegate lineShowLineColor];
        self.lineWidth = [self.delegate lineShowLineWidth];
    }else{
        UIColor *lineColor_temp = [[UIColor alloc] initWithRed:0.8f green:0.0f blue:0.859f alpha:1.9f];
        self.lineColor = lineColor_temp;
        self.lineWidth = 2;
    }

    //titleFont;
    if(self.delegate != nil &&
       [self.delegate respondsToSelector:@selector(lineShowTitleFont)]){
        self.titleFont = [self.delegate lineShowTitleFont];
    }else{
        self.titleFont = [UIFont fontWithName:@"Arial" size:20];
    }
    
    //lineDefualtColor
    if(self.delegate &&
       [self.delegate respondsToSelector:@selector(lineShowLineDefualtColor)]){
        self.lineDefualtColor = [self.delegate lineShowLineDefualtColor];
    }else{
        self.lineDefualtColor = [[UIColor alloc] initWithRed:0.8f green:0.8f blue:0.859f alpha:1.0f];
    }

    //linePointCount
    if(self.deletageSource != nil &&
       [self.deletageSource respondsToSelector:@selector(lineShowPointCount)] == YES){
        self.linePointCount = [self.deletageSource lineShowPointCount];
    }else{
        self.linePointCount = 0;
    }
    //lineEndPoint
    if(self.deletageSource &&
       [self.deletageSource respondsToSelector:@selector(lineShowEndPoint)]){
        self.lineEndPoint = [self.deletageSource lineShowEndPoint];
    }else{
        self.lineEndPoint = self.linePointCount;
    }
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    NSLog(@"drawRect");
    
    [self initData];
    
    NSMutableArray *array = [[NSMutableArray alloc]init];
    CGFloat theMaxValue = 0;
    [self getPointInfoArray:array theMaxValue:&theMaxValue];
    NSLog(@"array count:%ld,theMaxValue:%f",[array count],theMaxValue);
    
    CGFloat radius = 10;
    if (self.delegate != nil) {
        if([self.delegate respondsToSelector:@selector(lineShowPointRadius)]){
            radius = [self.delegate lineShowPointRadius];
        }
    }

    CGFloat theYOffset = self.titleFont.lineHeight + radius;
    CGFloat theScale = (8.0f/10.0f) * ((self.frame.size.height - theYOffset) / theMaxValue);

    //绘制title
    for(int j=0;j<[array count];j++){
        Alu_PointInfo *info = [array objectAtIndex:j];
        CGPoint p1 = CGPointMake(info.point.x, info.point.y*theScale);
        
        if(info.title != nil){
            [self drawText:info.title x:p1.x -self.titleFont.lineHeight
                         y:self.frame.size.height - self.titleFont.lineHeight];
        }
    }
    
    
    //绘图
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    
    //变化坐标系
    CGContextTranslateCTM(context, 0.0f, self.frame.size.height);
    CGContextScaleCTM(context, 1.0f, -1.0f);
    
    for (NSInteger i=0; i<[array count] -1 ; i++) {
        //绘制线条
        Alu_PointInfo *pointInfo =[array objectAtIndex:i];
        Alu_PointInfo *pointInfo2 = [array objectAtIndex:i+1];
        CGPoint p1 = CGPointMake(pointInfo.point.x, pointInfo.point.y*theScale +theYOffset);
        CGPoint p2 = CGPointMake(pointInfo2.point.x, pointInfo2.point.y*theScale+theYOffset);
        
//        if (i == 0) {
//            [self drawLineWithPoint:CGPointMake(0, 0+theYOffset) point2:p1 lineColor:self.lineColor];
//        }
        if( i == [array count] - 2){
            [self drawLineWithPoint:p2 point2:CGPointMake(self.frame.size.width, 0+theYOffset) lineColor:self.lineDefualtColor];
        }
        
        if(i < self.lineEndPoint - 1){
            [self drawLineWithPoint:p1 point2:p2 lineColor:self.lineColor];
        }else{
            [self drawLineWithPoint:p1 point2:p2 lineColor:self.lineDefualtColor];
        }
        
        //绘制点
        if (pointInfo.isDrawPoint) {
            [self drawPointWithPoint:p1 radius:radius color:pointInfo.pointColor];
        }
        if (i==[array count]-2) {
            [self drawPointWithPoint:p2 radius:radius color:pointInfo2.pointColor];
        }
        
//        //绘制文字
//        if(pointInfo.title != nil){
//            [self drawText:pointInfo.title x:p1.x y:p1.y];
//        }
    }
}

- (void)drawPointWithPoint:(CGPoint)p radius:(NSUInteger)radius color:(UIColor*) color
{
    CGFloat radius2 = radius*2/5;
    UIColor *color2 = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f];
    CGRect rect = CGRectMake(p.x-radius, p.y - radius, 2*radius, 2*radius);
    CGRect rect2 = CGRectMake(p.x-radius2, p.y - radius2, 2*radius2, 2*radius2);

    CGContextRef con = UIGraphicsGetCurrentContext();
    
    CGContextAddEllipseInRect(con,rect);
    CGContextSetFillColorWithColor(con, color.CGColor);
    CGContextFillPath(con);
    
    CGContextAddEllipseInRect(con,rect2);
    CGContextSetFillColorWithColor(con, color2.CGColor);
    CGContextFillPath(con);
}

- (void)drawLineWithPoint:(CGPoint) p1 point2:(CGPoint)p2 lineColor:(UIColor*)color
{
    //获得处理的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();

    
    //指定直线的样式
    CGContextSetLineCap(context,kCGLineCapSquare);
    
    //直线宽度
    CGContextSetLineWidth(context, self.lineWidth);
    
    
    //设置线条的颜色
    CGFloat r,g,b,a;
    if(color != nil){
        [color getRed:&r green:&g blue:&b alpha:&a];
    }else{
        [self.lineColor getRed:&r green:&g blue:&b alpha:&a];
    }
    CGContextSetRGBStrokeColor(context,r,g,b,a);
    
    //开始绘制
    CGContextBeginPath(context);
    
    //移动到第一个点
    CGContextMoveToPoint(context, p1.x, p1.y);
    
    //下一个点
    CGContextAddLineToPoint(context, p2.x, p2.y);
    
    //绘制完成
    CGContextStrokePath(context);
    NSLog(@"drawLineWithPoint end");
}

- (void) drawText:(NSString *)text x:(float)x y:(float)y
{
    
//    UIFont *font = [UIFont fontWithName:@"Arial" size:20];
    [text drawAtPoint:CGPointMake(x, y) withFont:self.titleFont];
}

- (void)updateLineShowView
{
    [self setNeedsDisplay];
}

- (void)clearLineShowView
{
    //获得处理的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextClearRect(context,self.frame);
}

- (void)getPointInfoArray:(NSMutableArray*)array theMaxValue:(CGFloat*)value
{
    NSInteger pointCount = self.linePointCount;
    NSInteger pointEnd = self.lineEndPoint;
    NSLog(@"pointCount:%ld",pointCount);
    
    CGFloat xGap = 50;
    xGap = self.frame.size.width / (pointCount+1);
    for (NSInteger i=0; i<pointCount; i++) {
        Alu_PointInfo *pointInfo = [[Alu_PointInfo alloc]init];
        CGFloat x = xGap*(i+1);
        CGFloat y = 0;
        if(self.deletageSource != nil){
            if([self.deletageSource respondsToSelector:@selector(lineShowValueInIndex:)]){
                y = [self.deletageSource lineShowValueInIndex:i];
            }
        }
        pointInfo.point = CGPointMake(x, y);
        NSLog(@"value:%f",y);
        
        if(*value < y){
            *value = y;
        }
        
        UIColor *color = [[UIColor alloc]initWithRed:10 green:10 blue:0 alpha:0.5];
        if(i <= pointEnd-1){
            if(self.delegate != nil){
                if([self.delegate respondsToSelector:@selector(lineShowPointColor)]){
                    color = [self.delegate lineShowPointColor];
                }
            }
        }else{
            if(self.delegate != nil &&
               [self.delegate respondsToSelector:@selector(lineShowPointDefualtColor)]){
                color = [self.delegate lineShowPointDefualtColor];
            }
        }
        pointInfo.pointColor = color;
        
        if(self.deletageSource != nil ){
            if([self.deletageSource respondsToSelector:@selector(lineShowNeedShowPointInIndex:)]){
                pointInfo.isDrawPoint = [self.deletageSource lineShowNeedShowPointInIndex:i];
            }else{
                pointInfo.isDrawPoint = NO;
            }
            
            if([self.deletageSource respondsToSelector:@selector(lineShowTitleInIndex:)]){
                pointInfo.title = [self.deletageSource lineShowTitleInIndex:i];
            }else{
                pointInfo.title = [NSString stringWithFormat:@"null"];
            }
        }
        
        [array addObject:pointInfo];
    }

}

@end
